import React from 'react';
import router from 'umi/router';
import moment from 'moment';

import { connect } from 'dva';

import { Button,Form, Select, Input,InputNumber,DatePicker } from 'antd';

const FormItem = Form.Item;

const { Option } = Select;

const linePath = '/HR/HrContract';

@connect(({
  hrContract, hrEmployee, hrPayrollStructure
}) => ({
  hrContract, hrEmployee, hrPayrollStructure

}))
class FormCreate extends React.Component {
  state = {
  }

  id = 0

  async componentDidMount() {
    const { location: {query: { id, employee_id } } } = this.props;

    this.id = Number(id)
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'hrContract/view',
        payload:  { id: this.id }
      });

    }

    // 获取多对一字段的下拉列表框选项所需要的数据

    const { dispatch } = this.props;
    await dispatch({
      type: 'hrPayrollStructure/search',
      payload: {domain:[] },
    });

    await dispatch({
      type: 'hrEmployee/search',
      payload: {domain:[] },
    });

    this.employee_id = Number(employee_id)

    await dispatch({
      type: 'hrEmployee/view',
      payload: {id:this.employee_id },
    });

  }

  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();

    const create_or_write = async (vals) => {
      if(this.id){
        await dispatch({
          type: 'hrContract/write',
          payload : {
            id: this.id,  vals,
          },
        });

        router.push(`${linePath}?id=${this.id}`);
      }
      else{
        const { hrEmployee: { record: employee } } = this.props;

        await dispatch({
          type: 'hrContract/create',
          payload : {
            vals: {...vals, employee_id: employee.id }
          },
        });

        const { hrContract:{record} } = this.props;

        if (record.id){
          router.push(`${linePath}?id=${record.id}`);
        }
      }

    }


    form.validateFieldsAndScroll( async (err, values) => {

      if (!err) {

        const { date_start } = values;

        const vals = { ...values,
          date_start: `${date_start.year()}-${date_start.month() + 1}-${date_start.date()}`,
        };

        await create_or_write(vals)

      }
    });
  };


  render() {
    const { form: { getFieldDecorator }  } = this.props;
    const { hrContract: { record } } = this.props;
    const { hrPayrollStructure: { recordsList: structList } } = this.props;
    const { hrEmployee: {
      recordsList: employeeList,
      record: employeeRecord,
    }} = this.props;

    return (
      <div>
          <Form onSubmit={this.handleSubmit}>
            <FormItem style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" >提交
              </Button>
            </FormItem>

            <div>
               员工: {employeeRecord.name}
            </div>


            <FormItem label="员工">
              {getFieldDecorator('employee_id', {
                rules: [{ required: true }],
                initialValue: record.employee_id && record.employee_id.id
              })(
                <Select placeholder="" >
                  <Option value={0} >未选择</Option>
                  {employeeList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {item.name}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>



            <FormItem label="名称">
              {getFieldDecorator('name', {
                rules: [{ required: true }],
                initialValue: record.name ,
              })(
                <Input />
              )}
            </FormItem>


            <FormItem label="开始日期">
              {getFieldDecorator('date_start', {
                rules: [{ required: true }],
                initialValue: record.date_start ? moment(record.date_start, 'YYYY-MM-DD') : moment()

              })(<DatePicker />)}
            </FormItem>


            <FormItem label="薪资结构">
              {getFieldDecorator('struct_id', {
                rules: [{ required: true }],
                initialValue: record.struct_id && record.struct_id.id
              })(
                <Select placeholder="" >
                  <Option value={0} >未选择</Option>
                  {structList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {item.name}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>


            <FormItem label="月薪">
              {getFieldDecorator('wage', {
                rules: [{ required: true }],
                initialValue: record.wage ,
              })(
                <InputNumber />
              )}
            </FormItem>

            <FormItem label="个税专项附加扣除">
              {getFieldDecorator('tax_deduction', {
                rules: [{ required: true }],
                initialValue: record.tax_deduction ,
              })(
                <InputNumber />
              )}
            </FormItem>



          </Form>


      </div>

    );
  }
}


export default Form.create()(FormCreate);


